package com.hydl.power.controller;

import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.utils.FtpUtil;
import com.hydl.power.utils.QiniuCloudUtil;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
@Api(value = "ImgController",tags = "图片路径")
@RestController
@RequestMapping("/img")
@CrossOrigin
public class ImgController {


    @ApiOperation(value = "用户签名图片上传", produces = "application/json")
    @PostMapping("uploadFile")
    public Resp uploadFile(@RequestParam("file")MultipartFile  file) throws IOException {
        Map<String, String> stringStringMap = FtpUtil.uploadFile("/userSignature", file.getOriginalFilename(), file.getInputStream());
        if(stringStringMap == null){
          return Resp.fail("上传失败");
        }
        return Resp.success(stringStringMap);
    }

    @ApiOperation(value = "运维签名图片上传", produces = "application/json")
    @PostMapping("maintainerUploadFile")
    public Resp maintainerUploadFile(@RequestParam("file")MultipartFile  file) throws IOException {
      Map<String, String> stringStringMap = FtpUtil.uploadFile("/maintainerSignature", file.getOriginalFilename(), file.getInputStream());
      if(stringStringMap == null){
        return Resp.fail("上传失败");
      }
      return Resp.success(stringStringMap);
    }


    @ApiOperation(value = "报警图片上传", produces = "application/json")
    @PostMapping("upLoad")
    public Resp upLoad(@RequestParam("file")MultipartFile  file) throws IOException {
        Map<String, String> stringStringMap = FtpUtil.uploadFile("/policeImage", file.getOriginalFilename(), file.getInputStream());
        if(stringStringMap == null){
            return Resp.fail("上传失败");
        }
        return Resp.success(stringStringMap);
    }




    //@LogAnnotation(module = "图片路径",description = "图片路径")
    @ResponseBody
    @RequestMapping(value="/uploadImg", method= RequestMethod.POST)
    public Resp uploadImg(@RequestParam MultipartFile image, HttpServletRequest request) {
        Resp result = new Resp();
        if (image.isEmpty()) {

            return Resp.fail();
        }

        try {
            byte[] bytes = image.getBytes();
            String imageName = UUID.randomUUID().toString();

            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                return Resp.success(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        } catch (IOException e) {

            return Resp.fail();
        }
    }
}
